var template = Handlebars.compile(applicationTemplate);

var loadApplication = function(application) {
	var tr = $('<tr/>').append(template(application));
	tr.find("option[value="+application.type+"]").attr("selected", "selected");
	$('#applicationList').append(tr);
}

$(function() {
	initHeader();
	$.getJSON('application', function(applicationList) {
		$.each(applicationList, function(index, application){
			loadApplication(application);
		})
	});
	$("div.valider").click(function(){
		var res = [];
		$("tr").each(function(){
			res.push({ 	"name" : $(this).find(".name").text(),
						"priorite" : $(this).find(".priorite").val(),
						"type" : $(this).find("option:selected").val()
					})
		});
		$.post('application',  {'json':JSON.stringify({'appList':res})}, function(){
			window.location.href = "applicationList.html";
		});
	});
});